<?php

/* Pop3 Functions.
  Smtp Functions. [Added later, going to split]

  For a real example take a look at:
  http://www.triple-it.nl/pop3/

  I have just started to explore the world of PHP3,
  so excuse (and correct me :) if I'm doing something wrong.

  Unk. (rgroesb@triple-it.nl)
 */

class EmailServer {

    function smtp_open($server, $port) {
        global $SMTP_GLOBAL_STATUS;

        $smtp = fsockopen($server, $port);
        if ($smtp < 0)
            return 0;

        $line = fgets($smtp, 1024);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] = substr($line, 0, 1);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULTTXT"] = substr($line, 0, 1024);

        if ($SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] <> "2")
            return 0;

        return $smtp;
    }

    function smtp_helo($smtp) {
        global $SMTP_GLOBAL_STATUS;

        /* 'localhost' always works [Unk] */
        fputs($smtp, "helo localhost\r\n");
        $line = fgets($smtp, 1024);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] = substr($line, 0, 1);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULTTXT"] = substr($line, 0, 1024);

        if ($SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] <> "2")
            return 0;

        return 1;
    }

    function smtp_ehlo($smtp) {
        global $SMTP_GLOBAL_STATUS;

        /* Well, let's use "helo" for now.. Until we need the
          extra func's [Unk]
         */
        fputs($smtp, "helo localhost\r\n");
        $line = fgets($smtp, 1024);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] = substr($line, 0, 1);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULTTXT"] = substr($line, 0, 1024);

        if ($SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] <> "2")
            return 0;

        return 1;
    }

    function smtp_mail_from($smtp, $from) {
        global $SMTP_GLOBAL_STATUS;

        fputs($smtp, "MAIL FROM: <$from>\r\n");
        $line = fgets($smtp, 1024);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] = substr($line, 0, 1);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULTTXT"] = substr($line, 0, 1024);

        if ($SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] <> "2")
            return 0;

        return 1;
    }

    function smtp_rcpt_to($smtp, $to) {
        global $SMTP_GLOBAL_STATUS;

        fputs($smtp, "RCPT TO: <$to>\r\n");
        $line = fgets($smtp, 1024);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] = substr($line, 0, 1);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULTTXT"] = substr($line, 0, 1024);

        if ($SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] <> "2")
            return 0;

        return 1;
    }

    function smtp_data($smtp, $subject, $data) {
        global $SMTP_GLOBAL_STATUS;

        fputs($smtp, "DATA\r\n");
        $line = fgets($smtp, 1024);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] = substr($line, 0, 1);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULTTXT"] = substr($line, 0, 1024);

        if ($SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] <> "3")
            return 0;

        fputs($smtp, "Mime-Version: 1.0\r\n");
        fputs($smtp, "Subject: $subject\r\n");
        fputs($smtp, "$data\r\n\r\n");
        fputs($smtp, ".\r\n");
        $line = fgets($smtp, 1024);
        if (substr($line, 0, 1) <> "2")
            return 0;

        return 1;
    }

    function smtp_quit($smtp) {
        global $SMTP_GLOBAL_STATUS;

        fputs($smtp, "QUIT\r\n");
        $line = fgets($smtp, 1024);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] = substr($line, 0, 1);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULTTXT"] = substr($line, 0, 1024);

        if ($SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] <> "2")
            return 0;

        return 1;
    }
    
    function imapOpen($user, $password){
        $server = "127.0.0.1";
        $host = "{" . $server ."}" . "INBOX";
        $msgstream = imap_open($host, $user, $password);
        return $msgstream;
    }
    
    function imapOpenOutBox($user, $password){
        $server = "127.0.0.1";
        $host = "{" . $server ."}" . "SENT";
        $msgstream = imap_open($host, $user, $password);
        return $msgstream;
    }
    
    function imapOpenRascunho($user, $password){
        $server = "127.0.0.1";
        $host = "{" . $server ."}" . "RASCUNHO";
        $msgstream = imap_open($host, $user, $password);
        return $msgstream;
    }

    function listDeletedEmailsImap($imap) {
        $emails = imap_search($imap,'DELETED');
        if($emails != false){
            rsort($emails);
        }
        return $emails;
    }
    
    function listEmailsImap($imap, $user) {
        $emails = imap_search($imap,'UNDELETED');
        if($emails != false){
            rsort($emails);
        }
        return $emails;
    }
    
    function listSendedEmailsImap($imap, $user) {
        $emails = imap_search($imap,'UNDELETED');
        if($emails != false){
            rsort($emails);
        }
        return $emails;
    }
    
    function obterInfoEmail($imap, $nr){
        return imap_fetch_overview($imap,$nr,0);
    }
    
    function obterConteudoEmail($imap, $nr){
        return imap_fetchbody($imap,$nr,1);
    }
    
    function deletarMensagem($imap, $nr){
        return imap_delete($imap, $nr);        
    }
    
    function imapClose($imap){
        return imap_close($imap);
    }

}

?>
